<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

class Shopping extends MX_Controller {

    function __construct() {

        parent::__construct();

//        $this->load->library('cart');
        $this->load->model('ticket/ticket_model');
    }

    /**
     * 
     */
    function add_to_cart() {
        if($this->phpsession->get('added_verify')) $this->phpsession->clear('added_verify');

        if ($this->is_postback() && $this->input->post('is_ajax')) {

            $ticket_id = $this->input->post('ticket_id');
            $cart_content = $this->cart->contents();
            //$cart_content = $this->phpsession->get('cart_contents');
            $data = array();
            
            if(!empty($ticket_id) && $onticket = $this->ticket_model->get_ticket(array('id'=>$ticket_id)))// && !empty($onticket[0]->quantity))
            {//
                //var_dump($cart_content);
                if($onticket[0]->quantity){
                    /*if (!empty($cart_content)) {
                        $rowid_string = '';
                        foreach($cart_content as $rowid=>$item){
                            if($item['id']==$ticket_id){
                                $rowid_string = $rowid;
                                break;
                            }
                        }
                        if(!empty($rowid_string)){
                            $data = array(
                                'id' => $ticket_id,
                                'qty' => $cart_content[$rowid_string]['qty'] + 1,
                                'price' => $cart_content[$rowid_string]['price'],
                                'name' => $cart_content[$rowid_string]['name'],
                                'maxquantity' => $onticket[0]->quantity,
                            );
                        }
                        else{
                            $data = array(
                                'id' => $ticket_id,
                                'qty' => 1,
                                'price' => $onticket[0]->price,
                                'name' => $onticket[0]->ticket_name,
                                'maxquantity' => $onticket[0]->quantity,
                            );
                        }                        
                        //$this->cart->update($data);
                        //echo 'VO DAY';
                        
                    } else {
                        //$ticket = $this->ticket_model->get_ticket(array('id' => $ticket_id));
                        $data = array(
                            'id' => $onticket[0]->id,
                            'qty' => 1,
                            'price' => $onticket[0]->price,
                            'name' => $onticket[0]->ticket_name,
                            'maxquantity' => $onticket[0]->quantity,
                        );
                        
                        //echo 'VO DAY INSERT'.print_r($data,1);
                    }*/
                    $data = array(
                            'id' => $onticket[0]->id,
                            'qty' => 1,
                            'price' => $onticket[0]->price,
                            'name' => $onticket[0]->ticket_name,
                            'maxquantity' => $onticket[0]->quantity,
                        );
                    $id_rows = $this->cart->insert($data);
                    if(!empty($id_rows)) {
                        $data['rowid'] = $id_rows;
                        $cart_content[$id_rows] = $data;
                        $this->phpsession->save('cart_contents', $cart_content);
                    }
                    
                    //var_dump($id_rows) ;
                    echo $this->cart->total_items();//.' -= '.$ticket_id;
                }
                else  echo __('ticket_unavailable');
                
            }
            else echo __('ticket_unavailable');

        } else {
            redirect('/');
        }
    }

    function update_cart() {
        //$this->phpsession->get('cart_content');
        //var_dump($cart_content);
        if(isset($_POST['qty']) && is_numeric($_POST['qty']) && $this->input->post('rowid')){
            $data = array(
                    'rowid' => $this->input->post('rowid'),
                    'qty' => $this->input->post('qty')
                );
            if($_POST['qty']>0){
                $cart_content = $this->cart->contents();
                if(!empty($cart_content[$this->input->post('rowid')])){
                    $item = $cart_content[$this->input->post('rowid')];
                    if($item['maxquantity']<$_POST['qty']){
                        echo json_encode(array('error_str'=>str_replace('[data]',$item['maxquantity'], __('ticket_quantity_over_quantity'))));
                    }
                    else{
                        $this->cart->update($data);
                        $cart_content = $this->cart->contents();
                        if(!empty($cart_content[$this->input->post('rowid')])){
                            $item = $cart_content[$this->input->post('rowid')];
                            echo json_encode(array('total_items'=>$this->cart->total_items(),'cart_total'=>__('&dollar;') . '&nbsp;' . $this->cart->format_number($this->cart->total()),'subtotal'=>__('&dollar;') . '&nbsp;' . $this->cart->format_number(($item['price']*$item['qty']))));
                        }
                        else {//for delete cart
                            echo json_encode(array('total_items'=>$this->cart->total_items(),'cart_total'=>__('&dollar;') . '&nbsp;' . $this->cart->format_number($this->cart->total())));
                        }
                    }
                }
                else{
                    echo json_encode(array('error_str'=>__('ticket_not_exists')));
                }
            }
            else{
                $this->cart->update($data);
                $cart_content = $this->cart->contents();
                if(!empty($cart_content[$this->input->post('rowid')])){
                    $item = $cart_content[$this->input->post('rowid')];
                    echo json_encode(array('total_items'=>$this->cart->total_items(),'cart_total'=>__('&dollar;') . '&nbsp;' . $this->cart->format_number($this->cart->total()),'subtotal'=>__('&dollar;') . '&nbsp;' . $this->cart->format_number(($item['price']*$item['qty']))));
                }
                else {//for delete cart
                    echo json_encode(array('total_items'=>$this->cart->total_items(),'cart_total'=>__('&dollar;') . '&nbsp;' . $this->cart->format_number($this->cart->total())));
                }
            }
            
            //$item = $cart_content[$this->input->post('rowid')];
            //var_dump($cart_content);
            //echo json_encode($cart_content[$this->input->post('rowid')]);
        }
        
        
        //if(!empty($ticket_id) && $onticket = $this->ticket_model->get_ticket(array('id'=>$ticket_id)) && !empty($onticket[0]->quantity)){
            
        //}
        //else echo __('ticket_unavailable');
        // Get the total number of items in cart
//        $total = $this->cart->total_items();

        // Retrieve the posted information
        /*$item = $this->input->post('rowid');
        $qty = $this->input->post('qty');
        //TODO: VALIDATE FORM HERE

        // Cycle true all items and update them
        for ($i = 0; $i < count($item); $i++) {
            // Create an array with the products rowid's and quantities. 
            $data = array(
                'rowid' => $item[$i],
                'qty' => $qty[$i]
            );

            // Update the cart with the new information
            $this->cart->update($data);
        }*/
        // redirect('shopping/show-cart');
    }

    //
    function show_cart() {
//        echo "Show cart is called";die;
        if($this->phpsession->get('added_verify')) $this->phpsession->clear('added_verify');
        return $this->load->view('shopping/frontend/show_cart');
    }

    function empty_cart() {
        
        $this->cart->destroy();
        
        if(!is_null ($this->phpsession->get('cart_content')))
            $this->phpsession->clear('cart_content');
        
        redirect('/');
    }

    
    
}
